﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Micro.Core.ApiConsumer.Context;

namespace Micro.Core.ApiConsumer.ParameterAttributes
{
    /// <summary>
    /// 接口地址参数，形式为：api/values?id={id}
    /// </summary>
    public sealed class RequestParamAttribute : ApiParameterAttribute
    {
        public override void Compose(HttpRequest request, ApiParameterDescriptor apiParameterDescriptor)
        {
            var name = apiParameterDescriptor.Name;
            var value = apiParameterDescriptor.Value;
            var valueString = value == null ? string.Empty : value.ToString();
            var regex = new Regex("{" + name + "}", RegexOptions.IgnoreCase);

            var pathQuery = request.Url.ToString();
            if (regex.IsMatch(pathQuery) == true)
            {
                request.Url = new Uri(regex.Replace(pathQuery, valueString));
            }
        }
    }
}
